% 读取已经去阴影和提取边界的二值图像
binary_image = imread('processed_edges.png'); % 替换为你的图像路径
if size(binary_image, 3) == 3
    binary_image = rgb2gray(binary_image); % 转换为灰度图（若非二值图）
end

% 霍夫变换检测直线
[H, theta, rho] = hough(binary_image); 
peaks = houghpeaks(H, 10, 'threshold', ceil(0.3 * max(H(:))));
lines = houghlines(binary_image, theta, rho, peaks, 'FillGap', 10, 'MinLength', 20);

% 可视化检测结果
figure;
imshow(binary_image);
hold on;

% 筛选竖线并提取 x 坐标
vertical_lines = [];
for k = 1:length(lines)
    line = lines(k);
    dx = abs(line.point2(1) - line.point1(1));
    dy = abs(line.point2(2) - line.point1(2));
    
    % 判断是否为竖线
    if dx < dy * 0.1 % 根据倾斜度筛选竖线
        x_coord = (line.point1(1) + line.point2(1)) / 2; % 线段中点 x 坐标
        vertical_lines = [vertical_lines; x_coord]; % 记录竖线 x 坐标
        plot([line.point1(1), line.point2(1)], [line.point1(2), line.point2(2)], 'LineWidth', 2, 'Color', 'red');
    end
end

% 计算左右竖线的间距
if length(vertical_lines) >= 2
    vertical_lines = sort(vertical_lines); % 按 x 坐标排序
    distance = abs(vertical_lines(end) - vertical_lines(1)); % 间距
    disp(['左右两条竖线的间距为: ', num2str(distance)]);
else
    disp('未检测到足够的竖线。');
end

hold off;
